from datetime import datetime
from src.common.config import config
from src.common.logger import getLogger
from src.agentic.rag.pattern.HYDERAG import HYDERAG
from src.modules.rag.service import document_service
from src.agentic.rag.pattern.StepBack import StepBack
from src.agentic.config.VectorStore import VectorStore
from src.agentic.rag.pattern.NativeRAG import NativeRAG
from src.agentic.rag.pattern.RAPTORRAG import RAPTORRAG
from src.agentic.rag.pattern.RAGFusion import RAGFusion
from src.agentic.rag.pattern.MultiQuery import MultiQuery
from src.agentic.config.LanguageModel import LanguageModel
from src.agentic.rag.pattern.SubQuestion import SubQuestion
from src.agentic.config.EmbeddingModel import EmbeddingModel
from src.agentic.rag.pattern.RoutingLogic import RoutingLogic
from src.agentic.rag.pattern.RoutingSemantic import RoutingSemantic
from src.agentic.rag.pattern.QueryConstruction import QueryConstruction
from src.agentic.rag.pattern.MultiRepresentation import MultiRepresentation
from src.modules.memory.service import HistoryRecordService, MemoryDetailService

logger = getLogger()

def invoke_retrieval_rag(form):
    start_time = datetime.now().second

    HistoryRecordService.insert_history_memory(form)

    base_url = "http://localhost:11434"
    llm_model = LanguageModel("qwen3:4b", base_url, None)
    llm = llm_model.new_llm_model()
    logger.info("invoke_retrieval_rag deepseek-v3.1:671b-cloud")

    embed_model = EmbeddingModel("bge-m3", base_url)
    embedding = embed_model.new_embed_model()

    config_dict = config.parse_config_key(["qdrant"])
    collection_prefix = config_dict.get("collection_prefix", "")
    logger.info(f"invoke_retrieval_rag collection_prefix: {collection_prefix}")

    vector_store = None
    library_number = form.get("library")
    logger.info(f"invoke_retrieval_rag library_number: {library_number}")
    if library_number:
        vector_store = VectorStore().new_vector_store(embedding, collection_prefix + library_number)

    rag_retriever = None
    ragPattern = form.get("pattern")
    logger.info(f"invoke_retrieval_rag ragPattern: {ragPattern}")
    if ragPattern == "NativeRAG":
        rag_retriever = NativeRAG(llm, vector_store)
    elif ragPattern == "MultiQuery":
        rag_retriever = MultiQuery(llm, vector_store)
    elif ragPattern == "RAGFusion":
        rag_retriever = RAGFusion(llm, vector_store)
    elif ragPattern == "SubQuestion":
        rag_retriever = SubQuestion(llm, vector_store)
    elif ragPattern == "StepBack":
        rag_retriever = StepBack(llm, vector_store)
    elif ragPattern == "HYDE":
        rag_retriever = HYDERAG(llm, vector_store)
    elif ragPattern == "RoutingLogic":
        document_list = document_service.select_document_list(None)
        rag_retriever = RoutingLogic(llm, embedding, collection_prefix, document_list)
    elif ragPattern == "RoutingSemantic":
        document_list = document_service.select_document_list(None)
        rag_retriever = RoutingSemantic(llm, embedding, collection_prefix, document_list)
    elif ragPattern == "QueryConstruction":
        rag_retriever = QueryConstruction(llm, vector_store)
    elif ragPattern == "MultiRepresentation":
        rag_retriever = MultiRepresentation(llm, embedding, collection_prefix, library_number)
    elif ragPattern == "RAPTOR":
        rag_retriever = RAPTORRAG(llm, embedding, collection_prefix, library_number)
    retrieve_result = rag_retriever.invoke(form.get("query"))

    MemoryDetailService.insert_memory_detail_ai(form, retrieve_result)

    end_time = datetime.now().second
    logger.info(f"invoke_retrieval_rag time: {end_time - start_time} s")
    return retrieve_result
